package com.code.factory.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiUtil {
    public final static int TYPE_MENU = 0x001;
    public final static int TYPE_FUNCTION = 0x002;
    public final static int TYPE_IDENTITY = 0x003;
    public final static int TYPE_DICT = 0x004;
    private final static DecimalFormat df = new DecimalFormat("##########");

    public static List<Map<String, Object>> parseXlsx(String path, int type) {
        try {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(path)));
            switch (type) {
            case TYPE_FUNCTION:
                list = loadFunctions(wb);
                break;
            case TYPE_MENU:
                list = loadMenus(wb);
                break;
            default:
                break;
            }
            return list;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }

    /**
     * 加载资源权限
     * 
     * @param wb
     * @return
     */
    public static List<Map<String, Object>> loadFunctions(XSSFWorkbook wb) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        XSSFSheet sheet = wb.getSheetAt(0);
        System.out.println(sheet.getPhysicalNumberOfRows());
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell cell = row.getCell(0);
            String cellValue = getValue(cell);
            if (!"".equals(cellValue) && cellValue != null) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", cellValue);
                map.put("name", getValue(row.getCell(1)));
                map.put("code", getValue(row.getCell(2)));
                map.put("sort", getValue(row.getCell(3)));
                list.add(map);
            }
        }
        return list;
    }

    /**
     * 加载菜单信息
     * 
     * @param wb
     * @return
     */
    public static List<Map<String, Object>> loadMenus(XSSFWorkbook wb) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//        for (int k = 0; k < 3; k++) {
            XSSFSheet sheet = wb.getSheetAt(0);
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                XSSFRow row = sheet.getRow(i);
                if (row.getCell(10) == null || !"CLOSED".equals(getValue(row.getCell(10)))) {
                    XSSFCell cell = row.getCell(0);
                    String cellValue = getValue(cell);
                    if (!"".equals(cellValue) && cellValue != null) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("id", cellValue);
                        map.put("name", getValue(row.getCell(1)));
                        map.put("identity_id", getValue(row.getCell(2)));
                        map.put("code", getValue(row.getCell(3)));
                        map.put("logo", getValue(row.getCell(4)));
                        map.put("sort", getValue(row.getCell(5)));
                        map.put("path", getValue(row.getCell(6)));
                        map.put("parent_id", getValue(row.getCell(7)));
                        String fn = getValue(row.getCell(8));
                        if("NULL".equals(fn)) {
                            map.put("functions", null);
                        } else {
                            map.put("functions", fn.split(","));
                        }
                        list.add(map);
                    }
                }
            }
//        }
        return list;
    }

    public static String getValue(XSSFCell cell) {
        String value = "";
        switch (cell.getCellType()) {
        case XSSFCell.CELL_TYPE_NUMERIC: // 数值型
            if (DateUtil.isCellDateFormatted(cell)) {
                // 如果是date类型则 ，获取该cell的date值
                value = DateUtil.getJavaDate(cell.getNumericCellValue()).toString();
            } else {// 纯数字
                value = df.format(cell.getNumericCellValue());
//                value = String.valueOf(cell.getNumericCellValue());
            }
            break;
        /* 此行表示单元格的内容为string类型 */
        case XSSFCell.CELL_TYPE_STRING: // 字符串型
            value = cell.getRichStringCellValue().toString();
            break;
        case XSSFCell.CELL_TYPE_FORMULA:// 公式型
            // 读公式计算值
            value = String.valueOf(cell.getNumericCellValue());
            if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
                value = cell.getRichStringCellValue().toString();
            }
            break;
        default:
            value = cell.getRichStringCellValue().toString();
            break;
        }
        return value;
    }

    public static void main(String[] args) {
        // List<Map<String, Object>> list =
        // PoiUtil.parseXlsx("D:\\SVN\\智慧社区DOC\\db\\资源权限表.xlsx",
        // PoiUtil.TYPE_FUNCTION);
        // for (Map<String, Object> map : list) {
        // System.out.println(map.get("name"));
        // }
        //List<Map<String, Object>> list = PoiUtil.parseXlsx("D:\\SVN\\滴滴快修\\菜单表.xlsx", PoiUtil.TYPE_MENU);
        //for (Map<String, Object> map : list) {
        //    System.out.println(map.get("name"));
        //}
    }
}
